// studentMine/setInput/setInput.js
import {
    ajaxPost,
    showToast
} from '../../utils/util'
let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        select: 0,
        type: '',
        key: '',
        placeholder: '',
        inputVal: '',
        newIndustryIndex: [0, 0],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let {
            type,
            val,
            title,
        } = options
        wx.setNavigationBarTitle({
            title: title
        })
        if (val == 'null') {
            val = ''
        }
        switch (type) {
            case 'name':
                this.setData({
                    inputVal: val,
                    type: type,
                    key: '公司全称',
                    placeholder: '输入您的公司全称'
                })
                break
            case 'short_name':
                this.setData({
                    inputVal: val,
                    type: type,
                    key: '公司简称',
                    placeholder: '输入您的公司简称'
                })
                break
            case 'address':
                this.setData({
                    inputVal: val,
                    type: type,
                    key: '公司地址',
                    placeholder: '输入您的公司地址'
                })
                break
            case 'link':
                this.setData({
                    inputVal: val,
                    type: type,
                    key: '公司官网',
                    placeholder: '输入您的公司官网'
                })
                break
            case 'description':
                this.setData({
                    select: 1,
                    inputVal: val,
                    type: type,
                    key: '公司简介',
                    placeholder: '输入您的公司简介'
                })
                break
            case 'welfare':
                this.setData({
                    select: 2,
                    type: type,
                })
                this.getCompanyInfo()
                break
            case 'industrySource':
                this.setData({
                    select: 3,
                    type: type,
                })
                this.getCompanyInfo()
                break

        }
    },

    /**
     * 获取公司信息
     */
    getCompanyInfo() {
        let data = {
            token: wx.getStorageSync("token"),
        }
        ajaxPost("/company/myCompanyInfo", data, (res) => {
            if (res.code == 0) {
                let {
                    company_info,
                    welfare_list,
                    scale_list,
                    nature_list,
                    industry_list,
                } = res.data
                app.globalData.companState = res.data.company_info.is_authentication
                let newIndustryOneArr = []
                for (let i in industry_list) {
                    newIndustryOneArr.push(industry_list[i].name)
                }
                let newIndustryTwoArr = []
                let industryTwoArr = industry_list[0].data
                for (let i in industryTwoArr) {
                    newIndustryTwoArr.push(industryTwoArr[i].name)
                }
                let newIndustryArr = [
                    newIndustryOneArr,
                    newIndustryTwoArr
                ]
                let welfare = res.data.company_info.welfare
                this.setData({
                    industrySource: industry_list,
                    newIndustryArr: newIndustryArr,
                    infoWelfareSource: welfare,
                    companySource: company_info,
                    welfareSource: welfare_list,
                    scaleSource: scale_list,
                    natureSource: nature_list,
                })
            } else {
                showToast('text', res.msg);
            }
        });
    },
    /**
     *  福利待遇选择
     * @param {*} e 传值
     */
    welfareClick(e) {
        let index = e.currentTarget.dataset.index
        let newWelfareSource = this.data.welfareSource
        let name = newWelfareSource[index].name
        let newInfoWelfareSource = this.data.infoWelfareSource
        if (newInfoWelfareSource.length == 8 && newWelfareSource[index].is_selected == 0) {
            showToast('text', '最多8个,请删除部分重新选择')
            return;
        }
        let indexof = newInfoWelfareSource.indexOf(name)
        if (indexof == -1) {
            newInfoWelfareSource.push(name);
        } else {
            newInfoWelfareSource.splice(indexof, 1);
        }
        newWelfareSource[index].is_selected == 1 ? newWelfareSource[index].is_selected = 0 : newWelfareSource[index].is_selected = 1
        this.setData({
            infoWelfareSource: newInfoWelfareSource,
            welfareSource: newWelfareSource
        })
    },
    /**
     * 
     * input 输入保存
     */
    bindKeyInput(e) {
        this.setData({
            inputVal: e.detail.value
        })
    },
    /**
     *  二级分类选择监听事件
     * @param {*} 穿参 
     */
    bindMultiPickerColumnChange(e) {
        let industryIndex = this.data.newIndustryIndex
        if (e.detail.column == 0 && industryIndex[0] !== e.detail.value) {
            let industrySource = this.data.industrySource
            let newTwoArr = []
            let arrTwo = industrySource[e.detail.value].data
            for (let i in arrTwo) {
                newTwoArr.push(arrTwo[i].name)
            }
            let Arr = [
                this.data.newIndustryArr[0],
                newTwoArr
            ]
            let newIndustryIndex = [e.detail.value, 0]
            this.setData({
                newIndustryArr: Arr,
                newIndustryIndex: newIndustryIndex,
            })
        }
    },
    /**
     * 保存信息
     */
    pushInfo() {
        let {
            type,
            inputVal,
        } = this.data
        if (type == 'name' || type == 'short_name' || type == 'address' || type == "link" || type == "description") {
            if (!inputVal) {
                showToast('error', '请填写完整信息');
                return;
            }
            let data = {
                [type]: inputVal
            }
            if (type == 'name') {
                this.setWYNickName(inputVal)
            }

            this.educationPushInfo(data)
            return;
        }
        if (type == 'welfare') {
            let infoWelfareSource = this.data.infoWelfareSource
            // let infoWelfareSource = []
            let data = {
                welfare: infoWelfareSource
            }
            this.educationPushInfo(data)
            return;
        }
        if (type == 'industrySource') {
            let {
                nature,
                scale,
                industry
            } = this.data.companySource
            let data = {
                nature: nature,
                scale: scale,
                industry: industry,
            }
            this.educationPushInfo(data)
            return;
        }
    },
    /**
     * 设置网易昵称
     */
    setWYNickName(val) {
        let data = {
            nickname: val,
            token: wx.getStorageSync('token'),
        }
        ajaxPost('/authuser/updateWyUserName', data, () => {});

    },
    /**
     * 学历信息保存
     * 
     */
    educationPushInfo(data) {
        data.token = wx.getStorageSync('token');
        ajaxPost("/company/updateCompanyInfo", data, (res) => {
            if (res.code == 0) {
                wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 1500,
                    success: () => {
                        setTimeout(() => {
                            wx.navigateBack()
                        }, 1500);
                    }
                })
            } else {
                showToast('text', res.msg);
            }
        });
    },
    /**
     * 时间改变
     */
    changeData(e) {
        let data = e.currentTarget.dataset
        let type = data.type
        let index = e.detail.value
        let newCompanySource = this.data.companySource
        switch (type) {
            case 'scale':
                /**
                 * 规模
                 */
                newCompanySource.scale = this.data.scaleSource[index]
                this.setData({
                    companySource: newCompanySource
                })

                break;
            case 'nature':
                /**
                 * 性质
                 */
                newCompanySource.nature = this.data.natureSource[index]
                this.setData({
                    companySource: newCompanySource
                })
                break;
            case 'industry':
                /**
                 * 性质
                 */
                let val = e.detail.value
                const industrySource = this.data.industrySource
                let oneArr = industrySource[val[0]].name
                let twoArr = industrySource[val[0]].data[val[1]].name
                newCompanySource.industry = []
                newCompanySource.industry.push(oneArr)
                newCompanySource.industry.push(twoArr)
                this.setData({
                    companySource: newCompanySource
                })
                break;


        }
    },
})